<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script>
    function testRange(){
        var html='';
        var selection = document.getSelection();
        var showRangeDiv = document.getElementById('showRange');
        if(selection.rangeCount>0){
            html = "您获取了"+selection.rangeCount+"段内容<br/>";
            for (var i= 0; i< selection.rangeCount; i++){
                var range = selection.getRangeAt(i);
                html += "第"+(i+1)+"段，内容是"+range;
            }
            showRangeDiv.innerHTML = html;
        }else{
            showRangeDiv.innerHTML = "您没有选择内容";
        }
    }
</script>



你好，我是range和selection
<input type="button" value="点我" onclick="testRange()">
<div id="showRange"></div>

</body>
</html>